﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class EntityManager : MonoBehaviour
{
    //缓存UI
    Dictionary<string, GameObject> m_Entities = new Dictionary<string, GameObject>();
    //UI分组
    Dictionary<string, Transform> m_Groups = new Dictionary<string, Transform>();

    private Transform m_EntityParent;

    private void Awake()
    {
        m_EntityParent = this.transform.parent.Find("Entity");
    }

    public void SetEntityGroup(List<string> group)
    {
        for(int i = 0; i < group.Count; i++)
        {
            GameObject go = new GameObject("Group-" + group[i]);
            go.transform.SetParent(m_EntityParent, false);
            m_Groups.Add(group[i], go.transform);
        }
    }
    Transform GetGroup(string group)
    {
        if (!m_Groups.ContainsKey(group))
            Debug.LogError("group is not exist");
        return m_Groups[group];
    }

    public void ShowEntity(string name,string group,string luaName)
    {
        //实现Awake特性
        GameObject entity = null;
        if(m_Entities.TryGetValue(name, out entity))
        {
            EntityLogic logic = entity.GetComponent<EntityLogic>();
            logic.OnShow();
            return;
        }
        //加载UI
        Manager.Resource.LoadPrefab(name, (UnityEngine.Object obj) =>
         {
             entity = Instantiate(obj) as GameObject;
             Transform parent = GetGroup(group);
             entity.transform.SetParent(parent, false);
             m_Entities.Add(name, entity);
             EntityLogic entityLogic = entity.AddComponent<EntityLogic>();
             entityLogic.Init(luaName);
             entityLogic.OnShow();
         });

    }
}
